package com.dao.impl;

import static com.def.GenericDef.E_XPBISQRS_MDL_NOT_FOUND;
import static com.def.GenericDef.E_XPBISQRS_NOT_FOUND_QRS;
import static com.def.GenericDef.E_XPBISQRS_QRS_ALREADY_EXIST;
import static com.def.GenericDef.RETURN_CODE_OK;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dao.IBisQrsDao;
import com.model.Bis_qrs;
import com.model.Bis_qrsId;
import com.tx.Xpbisqrs.XpbisqrsI;
import com.tx.Xpbisqrs.XpbisqrsIA;
import com.tx.Xpbisqrs.XpbisqrsO;
import com.tx.Xpbisqrs.XpbisqrsOA;
 
@Repository
public class BisQrsHibernateDao extends GenericHibernateDao<Bis_qrs, Bis_qrsId> 
implements IBisQrsDao
{
	public XpbisqrsO queryByCondition(XpbisqrsI xpbisqrsI){
		/***
		 * 暂支持 By id 查询
		 * **/
		 
		XpbisqrsO xpbisqrsO = new XpbisqrsO();
		xpbisqrsO.setTrx_id("XPBISQRS");
		xpbisqrsO.setType_id("O");
		List<XpbisqrsOA> oaryList = new ArrayList<XpbisqrsOA> ();
		
		XpbisqrsIA iary = xpbisqrsI.getIary().get(0);
		String hql ="FROM Bis_qrs where 1=1 ";
		if(iary.getMdl_id_fk()!=null){
			hql = hql + " and mdl_id_fk='" +  iary.getMdl_id_fk()  + "'";
		}
		if(iary.getMdl_typ_fk()!=null){
			hql = hql + " and mdl_typ_fk='" +  iary.getMdl_typ_fk()  + "'";
		}
		if(iary.getOpe_id_fk()!=null){
			hql = hql + " and ope_id_fk='" +  iary.getOpe_id_fk()  + "'";
		}
		if(iary.getOpe_no_fk()!=null){
			hql = hql + " and ope_no_fk='" +  iary.getOpe_no_fk()  + "'";
		}
		if(iary.getOpe_ver_fk()!=null){
			hql = hql + " and ope_ver_fk='" +  iary.getOpe_ver_fk()  + "'";
		}
		if(iary.getPath_id_fk()!=null){
			hql = hql + " and path_id_fk='" +  iary.getPath_id_fk()  + "'";
		}
		if(iary.getPath_ver_fk()!=null){
			hql = hql + " and path_ver_fk='" +  iary.getPath_ver_fk()  + "'";
		}
		if(iary.getTool_id_fk()!=null){
			hql = hql + " and tool_id_fk='" +  iary.getTool_id_fk()  + "'";
		}
		hql = hql + "ORDER BY mdl_typ_fk,mdl_id_fk,path_id_fk,ope_no_fk,ope_id_fk,ope_id_fk,tool_id_fk,qrs_id";
		List<Bis_qrs> bisQrsList =this.find( hql );
		for(Bis_qrs bis_qrs : bisQrsList){
			XpbisqrsOA oary = new XpbisqrsOA();
			oary.setMdl_id_fk(bis_qrs.getId().getMdl_id_fk());
			oary.setMdl_typ_fk(bis_qrs.getId().getMdl_typ_fk());
			oary.setOpe_id_fk(bis_qrs.getId().getOpe_id_fk());
			oary.setOpe_no_fk(bis_qrs.getId().getOpe_no_fk());
			oary.setOpe_ver_fk(bis_qrs.getId().getOpe_ver_fk());
			oary.setPath_id_fk(bis_qrs.getId().getPath_id_fk());
			oary.setPath_ver_fk(bis_qrs.getId().getPath_ver_fk());
			oary.setTool_id_fk(bis_qrs.getId().getTool_id_fk());
			
			oary.setQrs_id(bis_qrs.getId().getQrs_id());
			oary.setQrs_ope_id_fk(bis_qrs.getQrs_ope_id_fk());
			oary.setQrs_ope_no_fk(bis_qrs.getQrs_ope_no_fk());
			oary.setQrs_ope_ver_fk(bis_qrs.getQrs_ope_ver_fk());
			oary.setQrs_path_id_fk(bis_qrs.getQrs_path_id_fk());
			oary.setQrs_path_ver_fk(bis_qrs.getQrs_path_ver_fk());
			oary.setQrk_time(bis_qrs.getQrk_time());
			oary.setQrs_time(bis_qrs.getQrs_time());
			oary.setQrs_typ(bis_qrs.getQrs_typ());
			oary.setSwh_path_id_fk(bis_qrs.getSwh_path_id_fk());
			oary.setSwh_path_ver_fk(bis_qrs.getSwh_path_ver_fk());
			oaryList.add(oary);
		}
		xpbisqrsO.setOary(oaryList);
		xpbisqrsO.setTbl_cnt(oaryList.size());
		xpbisqrsO.setRtn_code(RETURN_CODE_OK);
		xpbisqrsO.setRtn_mesg("success");
		return xpbisqrsO;
	}
	
	public XpbisqrsO queryQrsKeyList(XpbisqrsI xpbisqrsI){
		 
		XpbisqrsO xpbisqrsO = new XpbisqrsO();
		xpbisqrsO.setTrx_id("XPBISQRS");
		xpbisqrsO.setType_id("O");
		List<XpbisqrsOA> oaryList = new ArrayList<XpbisqrsOA> ();
		
		XpbisqrsIA iary = xpbisqrsI.getIary().get(0);
		String hql ="FROM Bis_qrs where 1=1 ";
		if(iary.getMdl_id_fk()!=null){
			hql = hql + " and mdl_id_fk='" +  iary.getMdl_id_fk()  + "'";
		}
		if(iary.getMdl_typ_fk()!=null){
			hql = hql + " and mdl_typ_fk='" +  iary.getMdl_typ_fk()  + "'";
		}
		if(iary.getOpe_id_fk()!=null){
			hql = hql + " and ope_id_fk='" +  iary.getOpe_id_fk()  + "'";
		}
		if(iary.getOpe_no_fk()!=null){
			hql = hql + " and ope_no_fk='" +  iary.getOpe_no_fk()  + "'";
		}
		if(iary.getOpe_ver_fk()!=null){
			hql = hql + " and ope_ver_fk='" +  iary.getOpe_ver_fk()  + "'";
		}
		if(iary.getPath_id_fk()!=null){
			hql = hql + " and path_id_fk='" +  iary.getPath_id_fk()  + "'";
		}
		if(iary.getPath_ver_fk()!=null){
			hql = hql + " and path_ver_fk='" +  iary.getPath_ver_fk()  + "'";
		}
		if(iary.getTool_id_fk()!=null){
			hql = hql + " and tool_id_fk='" +  iary.getTool_id_fk()  + "'";
		}
		hql = hql + "ORDER BY mdl_typ_fk,mdl_id_fk,path_id_fk,ope_no_fk,ope_id_fk,ope_id_fk,tool_id_fk,qrs_id";
		List<Bis_qrs> bisQrsList =this.find( hql );
	    String mdl_id_fk="";
		String mdl_typ_fk="";
		String ope_id_fk="";
		String ope_no_fk="";
		String ope_ver_fk="";
		String path_id_fk="";
		String path_ver_fk="";
		String tool_id_fk="";
		for(Bis_qrs bis_qrs : bisQrsList){
			XpbisqrsOA oary = new XpbisqrsOA();
			if( mdl_id_fk.equals( bis_qrs.getId().getMdl_id_fk())
				&& mdl_typ_fk.equals( bis_qrs.getId().getMdl_typ_fk())
				&& ope_id_fk.equals(bis_qrs.getId().getOpe_id_fk())
				&& ope_no_fk.equals( bis_qrs.getId().getOpe_no_fk()) 
				&& ope_ver_fk.equals(bis_qrs.getId().getOpe_ver_fk())
				&& path_id_fk.equals(bis_qrs.getId().getPath_id_fk())
				&& path_ver_fk.equals(bis_qrs.getId().getPath_ver_fk())
				&& tool_id_fk.equals(bis_qrs.getId().getTool_id_fk())
			){
				continue;
			}
			oary.setMdl_id_fk(bis_qrs.getId().getMdl_id_fk());
			oary.setMdl_typ_fk(bis_qrs.getId().getMdl_typ_fk());
			oary.setOpe_id_fk(bis_qrs.getId().getOpe_id_fk());
			oary.setOpe_no_fk(bis_qrs.getId().getOpe_no_fk());
			oary.setOpe_ver_fk(bis_qrs.getId().getOpe_ver_fk());
			oary.setPath_id_fk(bis_qrs.getId().getPath_id_fk());
			oary.setPath_ver_fk(bis_qrs.getId().getPath_ver_fk());
			oary.setTool_id_fk(bis_qrs.getId().getTool_id_fk());
			
			oary.setQrs_id(bis_qrs.getId().getQrs_id());
			oary.setQrs_ope_id_fk(bis_qrs.getQrs_ope_id_fk());
			oary.setQrs_ope_no_fk(bis_qrs.getQrs_ope_no_fk());
			oary.setQrs_ope_ver_fk(bis_qrs.getQrs_ope_ver_fk());
			oary.setQrs_path_id_fk(bis_qrs.getQrs_path_id_fk());
			oary.setQrs_path_ver_fk(bis_qrs.getQrs_path_ver_fk());
			oary.setQrk_time(bis_qrs.getQrk_time());
			oary.setQrs_time(bis_qrs.getQrs_time());
			oary.setQrs_typ(bis_qrs.getQrs_typ());
			oary.setSwh_path_id_fk(bis_qrs.getSwh_path_id_fk());
			oary.setSwh_path_ver_fk(bis_qrs.getSwh_path_ver_fk());
			oaryList.add(oary);
			
			mdl_id_fk   = bis_qrs.getId().getMdl_id_fk();
			mdl_typ_fk  = bis_qrs.getId().getMdl_typ_fk();
			ope_id_fk   = bis_qrs.getId().getOpe_id_fk();
			ope_no_fk   = bis_qrs.getId().getOpe_no_fk(); 
			ope_ver_fk  = bis_qrs.getId().getOpe_ver_fk();
			path_id_fk  = bis_qrs.getId().getPath_id_fk();
			path_ver_fk = bis_qrs.getId().getPath_ver_fk();
			tool_id_fk  = bis_qrs.getId().getTool_id_fk() ;
		}
		xpbisqrsO.setOary(oaryList);
		xpbisqrsO.setTbl_cnt(oaryList.size());
		xpbisqrsO.setRtn_code(RETURN_CODE_OK);
		xpbisqrsO.setRtn_mesg("success");
		return xpbisqrsO;
	}
	public XpbisqrsO addProc(XpbisqrsI xpbisqrsI){
		XpbisqrsO xpbisqrsO = new XpbisqrsO();
		xpbisqrsO.setTrx_id("XPBISQRS");
		xpbisqrsO.setType_id("O");
		XpbisqrsIA iary = xpbisqrsI.getIary().get(0);
		Bis_qrsId id = new Bis_qrsId();
		id.setMdl_id_fk(iary.getMdl_id_fk());
		String mdl_typ_fk = getMdlTypByMdlIDProc(iary.getMdl_id_fk());
		if(mdl_typ_fk == null){
			xpbisqrsO.setRtn_code(E_XPBISQRS_MDL_NOT_FOUND+"");
			xpbisqrsO.setRtn_mesg("not found mdl_id["+iary.getMdl_id_fk() +"]");
			return xpbisqrsO;
		}
		id.setMdl_typ_fk(mdl_typ_fk);
		id.setOpe_id_fk(iary.getOpe_id_fk());
		id.setOpe_no_fk(iary.getOpe_no_fk());
		id.setOpe_ver_fk(iary.getOpe_ver_fk());
		id.setPath_id_fk(iary.getPath_id_fk());
		id.setPath_ver_fk(iary.getPath_ver_fk());
		id.setQrs_id(iary.getQrs_id());
		id.setTool_id_fk(iary.getTool_id_fk());
		Bis_qrs queryBisQrs = this.get(id);
		if( queryBisQrs != null){
			xpbisqrsO.setRtn_code(E_XPBISQRS_QRS_ALREADY_EXIST+"");
			xpbisqrsO.setRtn_mesg("Record already Exist");
			return xpbisqrsO;
		}
		Bis_qrs bis_qrs = new Bis_qrs();
		bis_qrs.setId(id);
		bis_qrs.setQrk_time(iary.getQrk_time());
		bis_qrs.setQrs_ope_id_fk(iary.getQrs_ope_id_fk());
		bis_qrs.setQrs_ope_no_fk(iary.getQrs_ope_no_fk());
		bis_qrs.setQrs_ope_ver_fk(iary.getQrs_ope_ver_fk());
		bis_qrs.setQrs_path_id_fk(iary.getQrs_path_id_fk());
		bis_qrs.setQrs_path_ver_fk(iary.getQrs_path_ver_fk());
		bis_qrs.setQrs_time(iary.getQrs_time());
		bis_qrs.setQrs_typ(iary.getQrs_typ());
		bis_qrs.setSwh_path_id_fk(iary.getSwh_path_id_fk());
		bis_qrs.setSwh_path_ver_fk(iary.getSwh_path_ver_fk());
		
        this.save(bis_qrs);
        
        xpbisqrsO.setRtn_code(RETURN_CODE_OK);
        xpbisqrsO.setRtn_mesg("success");
		return xpbisqrsO;
	}
	public XpbisqrsO updateProc(XpbisqrsI xpbisqrsI){
		XpbisqrsO xpbisqrsO = new XpbisqrsO();
		xpbisqrsO.setTrx_id("XPBISQRS");
		xpbisqrsO.setType_id("O");
		XpbisqrsIA iary = xpbisqrsI.getIary().get(0);
		Bis_qrsId id = new Bis_qrsId();
		id.setMdl_id_fk(iary.getMdl_id_fk());
		String mdl_typ_fk = getMdlTypByMdlIDProc(iary.getMdl_id_fk());
		if(mdl_typ_fk == null){
			xpbisqrsO.setRtn_code(E_XPBISQRS_MDL_NOT_FOUND+"");
			xpbisqrsO.setRtn_mesg("not found mdl_id["+iary.getMdl_id_fk() +"]");
			return xpbisqrsO;
		}
		id.setMdl_typ_fk(mdl_typ_fk);
		id.setOpe_id_fk(iary.getOpe_id_fk());
		id.setOpe_no_fk(iary.getOpe_no_fk());
		id.setOpe_ver_fk(iary.getOpe_ver_fk());
		id.setPath_id_fk(iary.getPath_id_fk());
		id.setPath_ver_fk(iary.getPath_ver_fk());
		id.setQrs_id(iary.getQrs_id());
		id.setTool_id_fk(iary.getTool_id_fk());
		Bis_qrs bis_qrs  = this.getDataByKeyProc(id);
		if(bis_qrs==null){
			xpbisqrsO.setRtn_code(E_XPBISQRS_NOT_FOUND_QRS+"");
			xpbisqrsO.setRtn_mesg("not Found Record[BIS_MDL_DEF]");
			return xpbisqrsO;
		}
		if(iary.getQrk_time()!=null){
			bis_qrs.setQrk_time(iary.getQrk_time());
		}
		if(iary.getQrs_ope_id_fk()!=null){
			bis_qrs.setQrs_ope_id_fk(iary.getQrs_ope_id_fk());
		}
		if(iary.getQrs_ope_no_fk()!=null){
			bis_qrs.setQrs_ope_no_fk(iary.getQrs_ope_no_fk());
		}
		if(iary.getQrs_ope_ver_fk()!=null){
			bis_qrs.setQrs_ope_ver_fk(iary.getQrs_ope_ver_fk());
		}
		if(iary.getQrs_path_id_fk()!=null){
			bis_qrs.setQrs_path_id_fk(iary.getQrs_path_id_fk());	
		}
		if(iary.getQrs_path_ver_fk()!=null){
			bis_qrs.setQrs_path_ver_fk(iary.getQrs_path_ver_fk());
		}
		if(iary.getQrs_time()!=null){
			bis_qrs.setQrs_time(iary.getQrs_time());
		}
		if(iary.getQrs_typ()!=null){
			bis_qrs.setQrs_typ(iary.getQrs_typ());
		}
		if(iary.getSwh_path_id_fk()!=null){
			bis_qrs.setSwh_path_id_fk(iary.getSwh_path_id_fk());	
		}
		if(iary.getSwh_path_ver_fk()!=null){
			bis_qrs.setSwh_path_ver_fk(iary.getSwh_path_ver_fk());
		}
		this.save(bis_qrs);
		xpbisqrsO.setRtn_code(RETURN_CODE_OK);
		xpbisqrsO.setRtn_mesg("success");
		return xpbisqrsO;
	}
	public XpbisqrsO deleteProc(XpbisqrsI xpbisqrsI){
		XpbisqrsO xpbisqrsO = new XpbisqrsO();
		xpbisqrsO.setTrx_id("XPBISQRS");
		xpbisqrsO.setType_id("O");
		XpbisqrsIA iary = xpbisqrsI.getIary().get(0);
		Bis_qrsId id = new Bis_qrsId();
		id.setMdl_id_fk(iary.getMdl_id_fk());
		String mdl_typ_fk = getMdlTypByMdlIDProc(iary.getMdl_id_fk());
		if(mdl_typ_fk == null){
			xpbisqrsO.setRtn_code(E_XPBISQRS_MDL_NOT_FOUND+"");
			xpbisqrsO.setRtn_mesg("not found mdl_id["+iary.getMdl_id_fk() +"]");
			return xpbisqrsO;
		}
		id.setMdl_typ_fk(mdl_typ_fk);
		id.setOpe_id_fk(iary.getOpe_id_fk());
		id.setOpe_no_fk(iary.getOpe_no_fk());
		id.setOpe_ver_fk(iary.getOpe_ver_fk());
		id.setPath_id_fk(iary.getPath_id_fk());
		id.setPath_ver_fk(iary.getPath_ver_fk());
		id.setQrs_id(iary.getQrs_id());
		id.setTool_id_fk(iary.getTool_id_fk());
		Bis_qrs bis_qrs  = this.getDataByKeyProc(id);
		if( bis_qrs == null ){
			xpbisqrsO.setRtn_code(E_XPBISQRS_NOT_FOUND_QRS+"");
			xpbisqrsO.setRtn_mesg("not Found Record[BIS_QRS]");
			return xpbisqrsO;
		}
		this.delete(bis_qrs);
		xpbisqrsO.setRtn_code(RETURN_CODE_OK);
		xpbisqrsO.setRtn_mesg("success");
		return xpbisqrsO;
	}
	public Bis_qrs getDataByKeyProc(Bis_qrsId id){
		/*****
		 *  String mdl_typ_fk,String mdl_id_fk,String path_id_fk,
			String path_ver_fk,String ope_no_fk ,String ope_id_fk,
	        String ope_ver_fk ,String tool_id_fk,String qrs_id 
		 * *******/
		Bis_qrs bisMdlDef = this.get(id);
		return bisMdlDef;
	}
	public XpbisqrsO deleteByCondition(XpbisqrsI xpbisqrsI){
		XpbisqrsO xpbisqrsO = new XpbisqrsO();
		xpbisqrsO.setTrx_id("XPBISQRS");
		xpbisqrsO.setType_id("O");
		XpbisqrsIA iary = xpbisqrsI.getIary().get(0);
		String hql ="FROM Bis_qrs where  mdl_id_fk='" +  iary.getMdl_id_fk()  + "'";
			hql = hql + " and mdl_typ_fk='" +  iary.getMdl_typ_fk()  + "'";
			hql = hql + " and ope_id_fk='" +   iary.getOpe_id_fk()  + "'";
			hql = hql + " and ope_no_fk='" +   iary.getOpe_no_fk()  + "'";
			hql = hql + " and ope_ver_fk='" +  iary.getOpe_ver_fk()  + "'";
			hql = hql + " and path_id_fk='" +  iary.getPath_id_fk()  + "'";
			hql = hql + " and path_ver_fk='" + iary.getPath_ver_fk()  + "'";
			hql = hql + " and tool_id_fk='" +  iary.getTool_id_fk()  + "'";
			
		List<Bis_qrs> bisQrsList =this.find( hql );
		for(Bis_qrs bis_qrs : bisQrsList){
			this.delete(bis_qrs);
		}

		xpbisqrsO.setRtn_code(RETURN_CODE_OK);
		xpbisqrsO.setRtn_mesg("success");
		return xpbisqrsO;
	}
	public String getMdlTypByMdlIDProc(String mdl_id ){
		String sql = "SELECT MDL_TYP FROM BIS_MDL_DEF WHERE MDL_ID='"+mdl_id +"'";
		List mdlTypeList =  this.findBySQL(sql);
		if(mdlTypeList.size()==0){
			return null;
		}
		return  mdlTypeList.get(0).toString();
	}	
}
